<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends CommonController {
    public function index(){
    	
    	$question = D('verify')->getVerify();

    	$this->assign('question',$question);
        $this->assign('title',登陆);
        $this->display();
    }

    public function cherkData()
    {	
    	$email = I('post.email');
    	$password = I('post.password');

    	//登陆时检测邮箱格式不正确时返回提示
		$preg='/^[A-z0-9]\w*@[A-z0-9-]+\.[A-z]{2,}(\.[A-z{2,}])*$/';
		$c=preg_match($preg, $email);
		if (!$c) {
			$arr = array('error'=>1,'info'=>'邮箱格式不正确!!!',);
    		echo json_encode($arr);exit;
		}

		//登陆时检测人类验证失败时返回提示
        $verify = I('post.verify');
        if ($verify!=$_SESSION['answer']) {
            $arr = array('error'=>1,'info'=>'人类验证失败!!!',);
            echo json_encode($arr);exit;
        }

		//登陆时检测 邮箱和密码 是正确时存SESSION 并提示成功  否则返回错误提示
    	if($res = D('user')->checkLogin($email,$password)){
    		//$res是二维数组
    		$_SESSION['id'] = current($res)['id'];
    		$_SESSION['nickname'] = current($res)['nickname'];
    		$_SESSION['face'] = current($res)['face'];

    		$arr = array('error'=>0,'info'=>'登陆成功!!',);
        	echo json_encode($arr);exit;

    	}else{
    		$arr = array('error'=>1,'info'=>'邮箱或密码错误!!!',);
    		echo json_encode($arr);exit;
    	}

    	

        //根据该邮箱查出对应的 昵称 存入SESSION
        //$res = M('user')->where("email='{$email}'")->find();
        //var_dump($res['nickname']);
        

        
    }

    public function outLogin()
    {
    	session_unset();
    	session_destroy();

    	$this->success('退出成功',U('home/index/index'),2);
    }

}